﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.Data
{
    public class MemuDal
    {
        #region 添加模型的菜单
        /// <summary>
        /// 添加模型的菜单
        /// </summary>
        /// <param name="modelId"></param>
        public void Add(byte? modelId)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;

                List<Memu> list = (from x in db.Memus
                                   where x.ModelId == modelId
                                   orderby x.RootId, x.OrderId
                                   select x).ToList();

                string newId = Guid.NewGuid().ToString();//内容管理节点下的模型id
                string newId2 = Guid.NewGuid().ToString();//内容模型节点下的模型id

                var maxModel = db.Models.OrderByDescending(x => x.Id).First();

                int maxRootId = db.Memus.Max(x => x.RootId) + 1;

                Memu[] newMemuList = new Memu[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    var v = list[i];
                    Memu r = new Memu();
                    if (v.Pid == "FED2B701-7295-47D9-8187-710D873463EA")
                    {
                        r.Id = newId;
                        r.Pid = "FED2B701-7295-47D9-8187-710D873463EA";
                        r.Title = maxModel.ModelName;
                    }
                    else if (v.Pid == "E3EA25FD-38A5-47B5-A1DF-34D0E8705A6F")
                    {
                        r.Id = newId2;
                        r.Pid = "E3EA25FD-38A5-47B5-A1DF-34D0E8705A6F";
                        r.Title = maxModel.ModelName;
                    }
                    else
                    {
                        r.Id = Guid.NewGuid().ToString();
                        r.Title = v.Title;
                    }
                    r.BaseModel = maxModel.BaseModel;
                    r.ModelId = maxModel.Id;
                    r.RootId = maxRootId;
                    r.OrderId = v.OrderId;

                    r.Link = v.Link;
                    r.OleId = v.Id;
                    r.OlePid = v.Pid;

                    newMemuList[i] = r;
                }
                //更换“内容模型”新下级的父id

                var y = newMemuList.FirstOrDefault(x => x.Id == newId2);
                if (y != null)
                    newMemuList.Where(x => x.OlePid == y.OleId).ToList().ForEach(x => x.Pid = y.Id);

                var z = newMemuList.FirstOrDefault(x => x.Title == "文章采集");
                if (z != null)
                    newMemuList.Where(x => x.OlePid == z.OleId).ToList().ForEach(x => x.Pid = z.Id);


                var gaoJi = newMemuList.FirstOrDefault(x => x.Title == "高级");
                if (gaoJi != null)
                    newMemuList.Where(x => x.OlePid == gaoJi.OleId).ToList().ForEach(x => x.Pid = gaoJi.Id);


                newMemuList.Where(x => x.Pid == null).ToList().ForEach(x => x.Pid = newId);

                newMemuList.ToList().ForEach(x => db.Memus.Add(x));

                db.SaveChangesAsync().Wait();

            }
        }
        #endregion

        /// <summary>
        /// 获取下级菜单
        /// </summary>
        /// <returns></returns>
        public IList<Memu> GetList()
        {
            using (Entities db = new Entities())
            {
                var query = db.Memus.Include(x => x.Model)
                    .Where(x => x.Model == null || x.Model.Hide == false)
                    .OrderBy(x => x.RootId).ThenBy(x => x.OrderId)
                    .ToList();
                query.ForEach(x =>
                {
                    if (x.Model == null)
                    {
                        x.ModelId = 0;
                    }
                    else
                    {
                        x.ModelId = x.Model.Id;
                    }
                });
                return query;
            }

        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="modelId"></param>
        public void Delete(byte? modelId)
        {
            using (Entities db = new Entities())
            {
                var q = db.Memus.Where(x => x.ModelId == modelId);
                q.ToList().ForEach(x => db.Memus.Remove(x));
                db.SaveChangesAsync().Wait();
            }
        }
    }
}